/*
 * @lc app=leetcode.cn id=922 lang=cpp
 *
 * [922] 按奇偶排序数组 II
 */

// @lc code=start
class Solution
{
public:
  vector<int> sortArrayByParityII(vector<int>& nums)
  {
    int n = nums.size();
    int eve_p = 0, odd_p = 1;
    for (; odd_p < n && eve_p < n; eve_p += 2, odd_p += 2) {
      while (eve_p < n && !(nums[eve_p] & 1))
        eve_p += 2;
      while (odd_p < n && nums[odd_p] & 1)
        odd_p += 2;
      if (eve_p < n && odd_p < n) {
        int tmp = nums[eve_p];
        nums[eve_p] = nums[odd_p];
        nums[odd_p] = tmp;
      }
    }
    return nums;
  }
};
// @lc code=end
